import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static java.util.Collections.swap;

public class CardDemo {
    public static final String[] suits={"♠","♥","♣","♦"};
    //生成52张牌
    public static List<Card> buyCard(){
        List<Card> cardList=new ArrayList<>();//存储生成的52张牌
        for (int i = 0; i < suits.length; i++) {
            for (int j = 1; j <=13 ; j++) {
                Card card=new Card(j,suits[i]);
                cardList.add(card);//将生成的牌存储到cardList中
            }
        }
        return cardList; //返回生成的52张牌
    }
    //洗牌
    public static void shuffle(List<Card> cardList){
        for (int i = cardList.size()-1; i>0; i--) {
            Random random=new Random();
           int indexOf= random.nextInt(i);
           swap(cardList,indexOf,i);
        }
    }
    public static void swap( List<Card> cardList,int x,int y){
        Card tmp=cardList.get(x);
        cardList.set(x,cardList.get(y));
        cardList.set(y,tmp);
    }

    public static void main(String[] args) {
       List<Card> cardList=buyCard();
        System.out.println(cardList); //生成52张牌
        System.out.println();
        System.out.println("洗牌");
        shuffle(cardList);
        System.out.println(cardList);
        System.out.println();
        System.out.println("揭牌");//3个人，每人轮流揭5张牌
        List<Card> hand0=new ArrayList<>();
        List<Card> hand1=new ArrayList<>();
        List<Card> hand2=new ArrayList<>();
        List<List<Card>>hands=new ArrayList<>(); //生成一个二维数组
        hands.add(hand0);
        hands.add(hand1);
        hands.add(hand2);//这三步是将hand0,1,2与hands建立联系，也就是hands的0，1，2下标里的值分别是一维数组hand0,1,2,方便管理
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card=cardList.remove(0);
                hands.get(j).add(card);
            }

        }
        System.out.println("打印第一个人的牌");
        System.out.println(hand0);
        System.out.println("打印第二个人的牌");
        System.out.println(hand1);
        System.out.println("打印第三个人的牌");
        System.out.println(hand2);
    }
}
